
import java.awt.*;
import javax.swing.*;

public class BallGame extends JFrame{

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    Image ball = Toolkit.getDefaultToolkit().getImage("image/ball.png");
    Image desk = Toolkit.getDefaultToolkit().getImage("image/desk.jpg");

    double x = 100;
    double y = 100;//定义小球的横纵坐标
    //	boolean right = true;//小球运动方向
    double degree = 3.14/3; //弧度，这里是60度

    //画窗口
    public void paint(Graphics g){
        g.drawImage(desk, 0, 0, null);
        g.drawImage(ball, (int)x,(int) y, null);

/*		if(right){
			x = x+10;
		}else{
			x = x-10;
		}
		if(x>786){   //856-40-30
			right = false;
		}
		if(x<40){    //40为桌子边框的宽度
			right = true;
		}
*/
        x = x+10*Math.cos(degree);
        y = y+10*Math.sin(degree);

        if(y>430||y<80){   //窗口高度40+边框40
            degree = -degree;
        }
        if(x>786||x<40){
            degree = degree-3.14;
        }
    }

    //窗口加载
    void launchFrame(){
        setSize(856,500);//桌面图片大小
        setLocation(50,50);//距离左侧，上放距离
        setVisible(true);

        //重画窗口
        while(true){
            repaint();
            try{
                Thread.sleep(40);//40ms.1秒=1000毫秒，大约一秒画25次窗口
            }catch(Exception e){
                e.printStackTrace();
            }

        }

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //System.out.println("hello,python");
        System.out.println("这是我第一个游戏！----2019/04/11");
        BallGame game = new BallGame();
        game.launchFrame();
    }

}
